package com.lbb.mvc.view

import android.view.View
import androidx.viewbinding.ViewBinding

class StatusController : AttachStatusController {

    private var layoutContentBinding: ViewBinding? = null
    private var layoutLoadingBinding: ViewBinding? = null
    private var layoutEmptyBinding: ViewBinding? = null
    private var layoutErrorBinding: ViewBinding? = null

    override fun onAttach(
        layoutContentBinding: ViewBinding?,
        layoutLoadingBinding: ViewBinding?,
        layoutEmptyBinding: ViewBinding?,
        layoutErrorBinding: ViewBinding?
    ) {
        this.layoutContentBinding = layoutContentBinding
        this.layoutLoadingBinding = layoutLoadingBinding
        this.layoutEmptyBinding = layoutEmptyBinding
        this.layoutErrorBinding = layoutErrorBinding
    }

    override fun onDetach() {

    }

    override fun isStatusView(): Boolean {
        return true
    }


    override fun showContentView() {

        layoutContentBinding?.root?.visibility = View.VISIBLE
        layoutLoadingBinding?.root?.visibility = View.INVISIBLE
        layoutEmptyBinding?.root?.visibility = View.INVISIBLE
        layoutErrorBinding?.root?.visibility = View.INVISIBLE
    }

    override fun showLoadingView() {

        layoutContentBinding?.root?.visibility = View.INVISIBLE
        layoutLoadingBinding?.root?.visibility = View.VISIBLE
        layoutEmptyBinding?.root?.visibility = View.INVISIBLE
        layoutErrorBinding?.root?.visibility = View.INVISIBLE
    }

    override fun showEmptyView() {

        layoutContentBinding?.root?.visibility = View.INVISIBLE
        layoutLoadingBinding?.root?.visibility = View.INVISIBLE
        layoutEmptyBinding?.root?.visibility = View.VISIBLE
        layoutErrorBinding?.root?.visibility = View.INVISIBLE
    }

    override fun showErrorView(listener: View.OnClickListener?) {

        layoutContentBinding?.root?.visibility = View.INVISIBLE
        layoutLoadingBinding?.root?.visibility = View.INVISIBLE
        layoutEmptyBinding?.root?.visibility = View.INVISIBLE
        layoutErrorBinding?.root?.visibility = View.VISIBLE
        listener?.let { errorListener ->
            layoutErrorBinding?.root?.setOnClickListener {
                showLoadingView()
                errorListener.onClick(layoutErrorBinding?.root)
            }
        }

    }
}